﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using DataAccess.FinancialManage;
using Ninject;
using WinForm.Helper;

namespace WinForm.FinancialManage
{
    public partial class MoneyInOutFrm : Form
    {
        private static readonly IKernel Kernel = new StandardKernel(new DbModule());
        private readonly IFinancial _financial = Kernel.Get<Financial>();

        public MoneyInOutFrm()
        {
            InitializeComponent();

            comboBoxEx1.SelectedIndex = 0;
            comboBoxEx2.SelectedIndex = 2;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            if (Converts.ConvertToInt(textBoxX1.Text) > 0)
            {
                ResultInfo<jsh_property> p = _financial.GetTopProperty(SystemInfo.OperateAddressId);
                if (p.ResultNumber == ResultEnum.Success)
                {
                    int v = Converts.ConvertToInt(((DevComponents.Editors.ComboItem)(comboBoxEx1.SelectedItem)).Value.ToString());
                    int t =
                        Converts.ConvertToInt(
                            ((DevComponents.Editors.ComboItem) (comboBoxEx2.SelectedItem)).Value.ToString());
                    if (v == 0)//入库
                    {
                        if(t==0)//现金
                        {
                            p.Obj.Inmoney = Converts.ConvertToInt(textBoxX1.Text);
                        }else if(v==1)//押金
                        {
                            p.Obj.Inreceiptmoney = Converts.ConvertToInt(textBoxX1.Text);
                        }else//其它
                        {
                            p.Obj.Fixmoney = Converts.ConvertToInt(textBoxX1.Text)+p.Obj.Fixmoney;
                        }
                    }
                    else if (v == 1)//出库
                    {
                        if (t == 0)//现金
                        {
                            p.Obj.Outmoney = Converts.ConvertToInt(textBoxX1.Text);
                        }
                        else if (v == 1)//押金
                        {
                            p.Obj.Outreceiptmoney = Converts.ConvertToInt(textBoxX1.Text);
                        }
                        else//其它
                        {
                            p.Obj.Fixmoney = p.Obj.Fixmoney-Converts.ConvertToInt(textBoxX1.Text);
                        }
                    }
                    else//其他
                    {

                    }
                    ResultInfo<int> op = _financial.CreateProperty(p.Obj.Incardcounts, p.Obj.Outcardcounts,
                                                                   p.Obj.Inmoney, p.Obj.Outmoney, p.Obj.Inreceiptmoney,
                                                                   p.Obj.Outreceiptmoney, p.Obj.Fixmoney, p.Obj.Fixcards,
                                                                   textBoxX2.Text);
                    if (op.ResultNumber == ResultEnum.Success)
                    {
                        MyMessageBox.ShowMessageBoxInfo("操作成功！");
                        this.Close();
                        this.Dispose();
                    }
                    else
                    {
                        errorMessage = op.Describe;
                    }
                }
                else
                {
                    errorMessage = p.Describe;
                }
            }
            else
            {
                errorMessage += "现金数目要大于0！";
            }
            if (!string.IsNullOrEmpty(errorMessage))
            {
                MyMessageBox.ShowMessageBoxError(errorMessage);
            }
        }
    }
}
